<template>
    <div>
        <el-select v-if="selectType === 'select'" :value="value" v-model="optionResult" @change="emitInput(value)">
            <el-option v-for="item in options" :key="item.id" :label="item.valueDesc" :value="item.id"></el-option>
        </el-select>
        <div v-if="selectType === 'radio'">
            <el-radio v-model="optionResult" v-for="item in options" :label="item.id" @change="emitInput(value)">{{item.valueDesc}}</el-radio>
        </div>
    </div>
</template>

<script>
    import Message from '../components/messages/index.js'
    export default {
        name: "SelectOption",
        props:{
            code:{
                type:String,
                default:""
            },
            value:{
                type:Number,
                defaults:0
            },
            selectType:{
               type:String,
               defaults:"select"
            }
        },
        data(){
            return {
                options:[],
                optionResult:""
            }
        },
        methods:{
            getOptions(data){
                if (data==null || data === ""){
                    Message.warning("请填写码表类别编号！")
                }else {
                    this.$http.get("/coder/getOptionsByCode/"+data).then(res => {
                        if (res.status === 200){
                            this.options = res.data;
                        }
                    }).catch(() => Message.error("获取Select选项失败!"))
                }
            },
            emitInput(val) {
                this.$emit('input', Number.parseInt(val));
            }
        },
        created(){
            this.getOptions(this.code)
        },
        watch:{
            "optionResult":function (data) {
                console.log("监听器");
                this.$emit('input', Number.parseInt(data));
            },
            "value":function (data) {
                this.optionResult = data;
            }
        }
    }
</script>

<style scoped>

</style>